package com.ztools.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;

import com.ztools.common.R;


public class ShadowLayout extends FrameLayout {
    private static final String TAG = ShadowLayout.class.getSimpleName();
    private Paint mShadowPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private RectF mBackgroundRectF = new RectF();
    private RectF mShadowRectF = new RectF();

    /**
     * 阴影的颜色, 需要带透明
     */
    private int mShadowColor;

    /**
     * 显示全部的圆角
     */
    private int mBackgroundColor;

    /**
     * 显示左部的阴影
     */
    private int mShadowPaddingLeft = 0;
    /**
     * 显示顶部的阴影
     */
    private int mShadowPaddingTop = 0;
    /**
     * 显示右部的阴影
     */
    private int mShadowPaddingRight = 0;
    /**
     * 显示低部的阴影
     */
    private int mShadowPaddingBottom = 0;


    /**
     * 显示全部的圆角
     */
    private int mBackgroundCornerRadius = 0;
    /**
     * 显示左部的圆角
     */
    private int mBackgroundCornerRadiusTopLeft = 0;
    /**
     * 显示顶部的圆角
     */
    private int mBackgroundCornerRadiusTopRight = 0;
    /**
     * 显示右部的圆角
     */
    private int mBackgroundCornerRadiusBottomLeft = 0;
    /**
     * 显示低部的圆角
     */
    private int mBackgroundCornerRadiusBottomRight = 0;
    /**
     * 设置羽化属性
     */
    private float mShadowBlurRadius;
    /**
     * 设置羽化属性
     */
    private int mShadowAlpha;

    public ShadowLayout(Context context) {
        this(context, null);
        init(null);
        initPaint();
    }

    public ShadowLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        init(attrs);
        initPaint();
    }

    public ShadowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
        initPaint();
    }

    /**
     * 读取设置的阴影的属性
     *
     * @param attrs 从其中获取设置的值
     */
    private void init(AttributeSet attrs) {
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);  // 关闭硬件加速,setShadowLayer 才会有效
        this.setWillNotDraw(false);                    // 调用此方法后，才会执行 onDraw(Canvas) 方法
        if (attrs == null) {
            return;
        }
        TypedArray ta = this.getContext().obtainStyledAttributes(attrs, R.styleable.common_ShadowLayout);
        mShadowColor = ta.getResourceId(R.styleable.common_ShadowLayout_common_shadow_color, Color.LTGRAY);
        mBackgroundColor = ta.getResourceId(R.styleable.common_ShadowLayout_common_background_color, Color.LTGRAY);
        mShadowBlurRadius = ta.getInteger(R.styleable.common_ShadowLayout_common_shadow_blur_radius, 0);
        mShadowAlpha = ta.getInteger(R.styleable.common_ShadowLayout_common_shadow_alpha, 255);
        mShadowPaddingLeft = initPadding(this.getPaddingLeft());
        mShadowPaddingTop = initPadding(this.getPaddingTop());
        mShadowPaddingRight = initPadding(this.getPaddingRight());
        mShadowPaddingBottom = initPadding(this.getPaddingBottom());
        mBackgroundCornerRadius = ta.getDimensionPixelSize(R.styleable.common_ShadowLayout_common_background_corner_radius, 0);
        if (mBackgroundCornerRadius == 0) {
            mBackgroundCornerRadiusTopLeft = ta.getDimensionPixelSize(R.styleable.common_ShadowLayout_common_background_corner_radius_top_left, 0);
            mBackgroundCornerRadiusTopRight = ta.getDimensionPixelSize(R.styleable.common_ShadowLayout_common_background_corner_radius_top_right, 0);
            mBackgroundCornerRadiusBottomLeft = ta.getDimensionPixelSize(R.styleable.common_ShadowLayout_common_background_corner_radius_bottom_left, 0);
            mBackgroundCornerRadiusBottomRight = ta.getDimensionPixelSize(R.styleable.common_ShadowLayout_common_background_corner_radius_bottom_right, 0);
        } else {
            mBackgroundCornerRadiusTopLeft = mBackgroundCornerRadius;
            mBackgroundCornerRadiusTopRight = mBackgroundCornerRadius;
            mBackgroundCornerRadiusBottomLeft = mBackgroundCornerRadius;
            mBackgroundCornerRadiusBottomRight = mBackgroundCornerRadius;
        }
        ta.recycle();
    }

    /**
     * 从初始化padding的尺寸
     *
     * @param shadowPadding
     */
    private int initPadding(int shadowPadding) {
        int mShadowPadding = shadowPadding;
        if (mShadowPadding < mShadowBlurRadius) {
            mShadowPadding = (int) (mShadowBlurRadius - mShadowPadding);
        }
        return mShadowPadding;
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {

        if (mShadowBlurRadius != 0) {
            //设置阴影的画笔
            mShadowPaint.setAntiAlias(true);
            mShadowPaint.setColor(ContextCompat.getColor(getContext(), mShadowColor));
            mShadowPaint.setStrokeWidth(1);
            mShadowPaint.setStyle(Paint.Style.FILL);
            mShadowPaint.setAlpha(mShadowAlpha);
            mShadowPaint.setMaskFilter(new BlurMaskFilter(mShadowBlurRadius, BlurMaskFilter.Blur.NORMAL));
            setLayerType(View.LAYER_TYPE_SOFTWARE, mShadowPaint);
        }
        //设置背景的画笔
        mBackgroundPaint.setAntiAlias(true);
        mBackgroundPaint.setColor(ContextCompat.getColor(getContext(), mBackgroundColor));
        mBackgroundPaint.setStrokeWidth(1);
        mBackgroundPaint.setStyle(Paint.Style.FILL);
        setLayerType(View.LAYER_TYPE_SOFTWARE, mBackgroundPaint);
    }


    /**
     * 决定View在ViewGroup中的位置 , 此处left ，top...是相对于父视图
     */
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    /**
     * 决定View的大小
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 如何绘制这个View, 真正绘制阴影的方法
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawShadow(canvas);
        drawBackground(canvas);
    }

    /**
     * 为 ShadowLayout 设置 Padding 以为显示阴影留出空间
     */
    private void drawShadow(Canvas canvas) {
        if (mShadowBlurRadius != 0) {
            float rectTop = mShadowPaddingTop;
            float rectLeft = mShadowPaddingLeft;
            float rectRight = this.getWidth() - mShadowPaddingRight;
            float rectBottom = this.getHeight() - mShadowPaddingBottom;
            mShadowRectF.left = rectLeft;
            mShadowRectF.top = rectTop;
            mShadowRectF.right = rectRight;
            mShadowRectF.bottom = rectBottom;
            canvas.drawRoundRect(mShadowRectF, mBackgroundCornerRadius, mBackgroundCornerRadius, mShadowPaint);
        }
    }


    /**
     * 为 ShadowLayout 设置 Padding 以为显示阴影留出空间
     */
    private void drawBackground(Canvas canvas) {
        float rectTop = getPaddingTop();
        float rectLeft = getPaddingLeft();
        float rectRight = this.getWidth() - getPaddingRight();
        float rectBottom = this.getHeight() - getPaddingBottom();
        mBackgroundRectF.left = rectLeft;
        mBackgroundRectF.top = rectTop;
        mBackgroundRectF.right = rectRight;
        mBackgroundRectF.bottom = rectBottom;
        canvas.drawRoundRect(mBackgroundRectF, mBackgroundCornerRadius, mBackgroundCornerRadius, mBackgroundPaint);
    }
}

